// 轮播图效果,注意不用修改此代码
import '../lib/layui/layui.js';
import { ajax, Login } from '../utils/ajax.js'
import '../lib/jquery.js'
import '../lib/layui/layui.js'

//自调用函数
(async () => {
    //判断是否登录
    //status不在data里啊！！！！
    let {status,user} = await Login();
    if (status == 1) {
        //变为不可见
        $('.off').removeClass('active');
        //变为一个你好
        $('.on').addClass('active');
        //获取服务器user的nickname
        $('.nickname').text(user.nickname);
        //个人中心可以点，触发的反应,跳转到个人中心
        $('.self').on('click', () => location.href = './self.html')

        //退出登录
        $('.logout').on('click', async () => {
            if (!confirm('确定退出？')) return;

            //获取本地信息
            let uid = localStorage.getItem('uid');
            let token = localStorage.getItem('token');

            //向服务器请求退出
            let { data: { message, code } } = await ajax.get('/users/logout', { params: { id: uid }, headers: { authorization: token } });

            if (code == 0) return alert('注销失败！');

            //否则注销成功
            localStorage.removeItem('uid');
            localStorage.removeItem('token');
            //切换登录显示
            $('.off').addClass('active');
            $('.on').removeClass('active')
        })
    }
})();

//渲染轮播图
async function render() {
    //获取轮播图列表
    let { data: { code, list } } = await ajax.get('/carousel/list');
    if (code != 1) return console.log('获取失败');

    let str = ``;
    //文档获取
    list.forEach(e => str += `<div><img src="${ajax.defaults.baseURL}/${e.name}"></div>`);
    //渲染
    $('#carousel > :first-child').html(str);

    // layui 渲染轮播图，动起来
    layui.carousel.render({
        elem: '#carousel', // 选择器
        width: '1200px', //设置容器宽度
        height: '600px',
        arrow: 'hover',
        anim: 'fade' //切换动画方式
    });
}
render();